package com.myres.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.model.Product;
import com.myres.product.db.ProductService;

public class getProducts extends ServiceServlet {

	public void doRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		int cid=Integer.parseInt(request.getParameter("cid"));;
		ProductService service=new ProductService();
		List<Product> products=service.getProductsOfCategory(cid);
		StringBuilder builder=new StringBuilder();
		builder.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
		builder.append("<response>");
		builder.append("<products>");
		for(Product product:products){
			builder.append("<product>");
			builder.append("<p_id>");
			builder.append(product.getId());
			builder.append("</p_id>");
			builder.append("<p_name>");
			builder.append(product.getName());
			builder.append("</p_name>");
			builder.append("<p_price>");
			builder.append(product.getPrice());
			builder.append("</p_price>");
			builder.append("<p_description>");
			if(product.getDescription()!=null)builder.append(product.getDescription());
			builder.append("</p_description>");
			builder.append("<p_icon>");
			if(product.getIcon()!=null)builder.append(product.getIcon());
			builder.append("</p_icon>");
			builder.append("</product>");
		}
		builder.append("</products>");
		builder.append("</response>");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(builder.toString());
	}

}
